package com.liuya.db.sql;

import java.util.Map;

import com.liuya.safe.exception.AuthException;
import com.liuya.safe.model.SafeUser;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 上下文
 */
public class ContextValue implements Value, java.io.Serializable  {
	private static Log log = LogFactory.getLog(ContextValue.class);
	private String key;
	private boolean behindLike;

	public boolean isBehindLike() {
		return behindLike;
	}

	public void setBehindLike(boolean behindLike) {
		this.behindLike = behindLike;
	}

	public String toSQL() {
		return " ? ";
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public Object getValue(SafeUser user, Map context) {
		if (!context.containsKey(key)) {
			String msg = "No '" + key + "' found in context.";
			log.error(msg);
			throw new AuthException("No '" + key + "' found in context.");
		}
		return context.get(key);
	}
}
